package com.globaldom.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class Facebook {

	// remote server
	// private static final String api_key = "04a0fe96765c10603ce6b7dcf7ef89e0";
	// private static final String secret = "ef365f680464ca76b3e245a67650af89";
	// private static final String client_id = "230998268897";
	// private static final String redirect_uri =
	// "http://apps.facebook.com/globaldominationxxx/";

	// LOCAL
	private static final String api_key = "0bef528b7c0fb390bc2d75112be60b92";
	private static final String secret = "43f82864988c4f6196fa04be5b690133";
	private static final String client_id = "128354671037";
	private static final String redirect_uri = "http://apps.facebook.com/dateeng/";

	public static String getClientId() {
		return client_id;
	}

	public static String getAPIKey() {
		return api_key;
	}
	public static String getSecret() {
		return secret;
	}
	public static String getLoginRedirectURL() {
		return "https://graph.facebook.com/oauth/authorize?client_id=" + client_id + "&redirect_uri=" + redirect_uri
				+ "&scope=user_status,publish_stream";
	}
	public static String getAuthURL(String authCode) {
		return "https://graph.facebook.com/oauth/access_token?canvas=1&fbconnect=0&client_id=" + client_id
				+ "&redirect_uri=" + redirect_uri + "&client_secret=" + secret + "&code=" + authCode;
	}

	public String getToken(String code) {
		String encodedCode = "";
		String retStr;
		try {
			encodedCode = URLEncoder.encode(code, "UTF-8");
			String u1 = "https://graph.facebook.com/oauth/access_token?" + "client_id=" + client_id + "&"
					+ "redirect_uri=" + redirect_uri + "&" + "client_secret=" + secret + "&" + "type=user_agent&"
					+ "code=" + encodedCode;
			retStr = fetchURL(u1);
		} catch (UnsupportedEncodingException e) {
			retStr = null;
		}
		return retStr;
	}

	public String fetchURL(String u) {
		String retStr = "";
		try {
			URL url = new URL(u);
			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
			String line;
			while ((line = reader.readLine()) != null) {
				retStr += line;

			}
			reader.close();

		} catch (MalformedURLException e) {
			// ...
		} catch (IOException e) {
			// ...
		}
		return retStr;

	}

}
